package org.sunnyleaf.votedemo.client.ui;

import java.util.Vector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;

import org.sunnyleaf.votedemo.client.data.*;

public class VoteResultPage extends Composite {
	private Vote vote;
	private Vector<Vector<Integer>> selectInfo = new Vector< Vector<Integer> >();
	
	public VoteResultPage( Vote v ){
		this.vote = v;
		VerticalPanel mainVp = new VerticalPanel();
		mainVp.setWidth("100%");
		mainVp.add( new HTML("标题:" + v.getVoteName() ) );
		int index = 0;
		selectInfo.setSize( v.getVoteItems().size());
		for( VoteItem item: v.getVoteItems()){
			mainVp.add(new VoteItemOption(index++,item));
		}
		
		Button closeButton =  new Button("收起", new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				deleteSelf();
			}
		});
	
		mainVp.add( closeButton );
		mainVp.setSpacing(15);
		mainVp.setCellHorizontalAlignment( closeButton, mainVp.ALIGN_RIGHT );
		
		this.initWidget(mainVp);
	}
	
	// 删除自身界面
	private void deleteSelf(){
		Panel parent = (Panel)this.getParent();
		parent.remove(this);
	}
	
	class VoteItemOption extends Composite {
	
		private int instanceId;
			
		public VoteItemOption( int index,  VoteItem item ) {
			this.instanceId = index;
			FlexTable ft = new FlexTable();
			ft.setStyleName("voteopinion-border");
			ft.setWidth("100%");
			ft.setCellPadding(3);
			ft.setCellSpacing(3);
			
			ft.setWidget( 0,0, new Label("问题"+(instanceId+1)+": "+item.getQuestion()));
			
			// 所有票数
			int countsAll = 0;
			for( int i: item.getCountsArray() ){
				countsAll+=i;
			}
			
			int i = 0;
			for( String itemName : item.getItemArray()){	
				Label itemLabel = new Label( itemName );
				ft.setWidget(i+1, 0, itemLabel);
				Label itemInfo = new Label();
				int count = item.getCountsArray().get(i);
				itemInfo.setText( "得票: "+ count + "( %"+(float)count/countsAll*100+" )");
				ft.getCellFormatter().setHorizontalAlignment(i+1, 1, HasHorizontalAlignment.ALIGN_CENTER);
				ft.setWidget(i+1,1, itemInfo);
				i++;
			}

			initWidget(ft);
		}
	}
}
